package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.UmsAdminDTO;
import com.macro.mall.dto.UmsPlantingTechnologyDto;
import com.macro.mall.mapper.*;
import com.macro.mall.model.*;
import com.macro.mall.service.UmsPlantingTechnologyService;
import local.com.quyum.aliyun.aliyunoss.service.VodService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UmsPlantingTechnologyServiceImpl implements UmsPlantingTechnologyService {

    @Autowired
    private UmsPlantingTechnologyTypeMapper plantingTypeMapper;
    @Autowired
    private UmsPlantingTechnologyMapper plantingMapper;
    //    @Autowired
//    private UmsVideoMapper videoMapper;
    @Autowired
    private PmsPlantingTechnologyProductMapper plantingProductMapper;
    @Autowired
    private PmsSkuStockMapper skuStockMapper;
    @Autowired
    private VodService vodService;

    @Override
    public int creatType(UmsPlantingTechnologyType umsPlantingTechnologyType) {
        return plantingTypeMapper.insertSelective(umsPlantingTechnologyType);
    }

    @Override
    public int deleteType(Long id) {
        return plantingTypeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<UmsPlantingTechnologyType> getPlantingTechnologyType(Long storeId, String type, Integer pageSize, Integer pageNum) {
        UmsPlantingTechnologyTypeExample example = new UmsPlantingTechnologyTypeExample();
        UmsPlantingTechnologyTypeExample.Criteria criteria = example.createCriteria();
        criteria.andStoreIdEqualTo(storeId);
        if (type != null && !"".equals(type)) {
            criteria.andTypeLike("%" + type + "%");
        }
        example.setOrderByClause("id desc");
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<UmsPlantingTechnologyType> umsPlantingTechnologyTypes = plantingTypeMapper.selectByExample(example);
        return umsPlantingTechnologyTypes;
    }

    @Override
    public UmsPlantingTechnologyType getType(Long id) {
        return plantingTypeMapper.selectByPrimaryKey(id);
    }

    @Override
    public int creat(UmsPlantingTechnologyDto umsPlantingTechnologyDto, UmsAdminDTO umsAdminDTO) {
        if (umsAdminDTO == null) {
            return 0;
        }
        UmsPlantingTechnology umsPlantingTechnology = new UmsPlantingTechnologyDto();
        BeanUtils.copyProperties(umsPlantingTechnologyDto, umsPlantingTechnology);
        List<Long> productIds = umsPlantingTechnologyDto.getProductIds();
        umsPlantingTechnology.setStoreId(umsAdminDTO.getStoreId());
        umsPlantingTechnology.setCreateTime(new Date());
        int count = plantingMapper.insert(umsPlantingTechnology);
        //添加种植技术商品
        if (productIds != null && productIds.size() > 0) {
            for (Long productId : productIds) {
                PmsPlantingTechnologyProduct product = new PmsPlantingTechnologyProduct();
                product.setPlantingId(umsPlantingTechnology.getId());
                product.setProductId(productId);
                plantingProductMapper.insert(product);
            }
        }
        return count;
    }

    @Override
    public int updateType(UmsPlantingTechnologyType umsPlantingTechnologyType) {
//        umsPlantingTechnologyType.setId(id);
        return plantingTypeMapper.updateByPrimaryKeySelective(umsPlantingTechnologyType);
    }


    @Override
    public CommonPage<UmsPlantingTechnologyDto> getPlantList(Long storeId, String keyWord, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        UmsPlantingTechnologyExample example = new UmsPlantingTechnologyExample();
        UmsPlantingTechnologyExample.Criteria criteria = example.createCriteria();
        criteria.andStoreIdEqualTo(storeId);
        if (!StringUtils.isEmpty(keyWord)) {
            criteria.andTitleLike("%" + keyWord + "%");
        }
        example.setOrderByClause("create_time desc");
        List<UmsPlantingTechnology> umsPlantingTechnologys = plantingMapper.selectByExampleWithBLOBs(example);
        CommonPage<UmsPlantingTechnology> umsPlantingTechnologyCommonPage = CommonPage.restPage(umsPlantingTechnologys);
        List<UmsPlantingTechnology> umsPlantingTechnologyList = umsPlantingTechnologyCommonPage.getList();

        List<UmsPlantingTechnologyDto> result = new ArrayList<>();
        for (UmsPlantingTechnology umsPlantingTechnology : umsPlantingTechnologyList) {
            PmsPlantingTechnologyProductExample planExample = new PmsPlantingTechnologyProductExample();
            planExample.createCriteria().andPlantingIdEqualTo(umsPlantingTechnology.getId());
            List<PmsPlantingTechnologyProduct> pmsPlantingTechnologyProducts = plantingProductMapper.selectByExample(planExample);
            List<Long> productIds = pmsPlantingTechnologyProducts.stream().map(PmsPlantingTechnologyProduct::getProductId).collect(Collectors.toList());
            UmsPlantingTechnologyDto dto = new UmsPlantingTechnologyDto();
            BeanUtils.copyProperties(umsPlantingTechnology, dto);
            dto.setProductIds(productIds);
            if(org.apache.commons.lang3.StringUtils.isNotBlank(umsPlantingTechnology.getVideoId())){
                dto.setVideoUrl(this.vodService.getVideoUrl(umsPlantingTechnology.getVideoId()));

            }
            result.add(dto);
        }
        CommonPage<UmsPlantingTechnologyDto> resultPage = new CommonPage<UmsPlantingTechnologyDto>();
        BeanUtils.copyProperties(umsPlantingTechnologyCommonPage, resultPage);
        resultPage.setList(result);
        return resultPage;
    }

    @Override
    public CommonResult updatePlant(UmsPlantingTechnologyDto umsPlantingTechnologyDto) {
//        umsPlantingTechnology.setId(id);
        UmsPlantingTechnology umsPlantingTechnology = new UmsPlantingTechnologyDto();
        BeanUtils.copyProperties(umsPlantingTechnologyDto, umsPlantingTechnology);
        if(org.apache.commons.lang3.StringUtils.isNotBlank(umsPlantingTechnologyDto.getVideoId())){
            umsPlantingTechnology.setVideoUrl(this.vodService.getVideoUrl(umsPlantingTechnologyDto.getVideoId()));
        }
        int count = plantingMapper.updateByPrimaryKeyWithBLOBs(umsPlantingTechnology);
        List<Long> productIds = umsPlantingTechnologyDto.getProductIds();
        //删除之前绑定的商品
        PmsPlantingTechnologyProductExample example = new PmsPlantingTechnologyProductExample();
        example.createCriteria().andPlantingIdEqualTo(umsPlantingTechnologyDto.getId());
        plantingProductMapper.deleteByExample(example);
        //把商品重新加回去
        if (!CollectionUtils.isEmpty(productIds)) {
            for (long productId : productIds) {
                PmsPlantingTechnologyProduct product = new PmsPlantingTechnologyProduct();
                product.setPlantingId(umsPlantingTechnology.getId());
                product.setProductId(productId);
                plantingProductMapper.insertSelective(product);
            }
        }
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @Override
    public CommonResult deletePlant(Long id) {
        int count = plantingMapper.deleteByPrimaryKey(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @Override
    public CommonResult deletePlantList(List<Long> ids) {
        UmsPlantingTechnologyExample example = new UmsPlantingTechnologyExample();
        example.createCriteria().andIdIn(ids);
        int count = plantingMapper.deleteByExample(example);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
}
